# -*- coding: UTF-8 -*-
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject


class Move:
    STEP = -15
    JUMPTIME = 60
    def __init__(self,cam,game_obj,player):
        self.cam = cam
        self.player = player
        self.game_obj = game_obj
        pass
    
    def initMove(self):
        self.isMoving = False
    
    def setKey(self, key, value):
        self.game_obj.keyMap[key] = value
    
    def runMove(self):
        if (self.game_obj.keyMap["space"]!=0):
            #print self.player.ralph.getPos()
            #如果在地面
            if self.player.isJump == False:
                self.player.jumperTimer=Move.JUMPTIME
                self.player.start_z = self.player.ralph.getZ() 
                self.player.isJump = True
            #如果正在跳跃
        if self.player.isJump == True:
            self.player.jumperTimer = self.player.jumperTimer-1
            if self.player.jumperTimer == 0:
                self.player.isJump = False
            self.player.jump()
            
        startpos = self.player.ralph.getPos()
        move_click = True
        if (self.game_obj.keyMap["right"]!=0 and self.game_obj.keyMap["backward"]!=0):
            self.player.ralph.setH(-135)
            move_click = True
        elif (self.game_obj.keyMap["left"]!=0 and self.game_obj.keyMap["backward"]!=0):
            self.player.ralph.setH(135) 
        elif (self.game_obj.keyMap["right"]!=0 and self.game_obj.keyMap["forward"]!=0):
            self.player.ralph.setH(-45)  
        elif (self.game_obj.keyMap["left"]!=0 and self.game_obj.keyMap["forward"]!=0):
            self.player.ralph.setH(45) 
        elif (self.game_obj.keyMap["forward"]!=0): 
            self.player.ralph.setH(0)
        elif (self.game_obj.keyMap["backward"]!=0):
            self.player.ralph.setH(-180)      
        elif (self.game_obj.keyMap["left"]!=0):
            self.player.ralph.setH(90) 
        elif (self.game_obj.keyMap["right"]!=0):
            self.player.ralph.setH(-90)
        else:
            move_click = False
        if(move_click==True):
            self.player.ralph.setY(self.player.ralph, Move.STEP* globalClock.getDt())  
            
        # If ralph is moving, loop the run animation.
        # If he is standing still, stop the animation.

        if (self.game_obj.keyMap["left-shift"]!=0) or (self.game_obj.keyMap["right-shift"]!=0) or (self.game_obj.keyMap["forward"]!=0) or (self.game_obj.keyMap["backward"]!=0) or (self.game_obj.keyMap["left"]!=0) or (self.game_obj.keyMap["right"]!=0):
            if self.isMoving is False:
                self.player.ralph.loop("run")
                self.isMoving = True
        else:
            if self.isMoving:
                self.player.ralph.stop()
                self.player.ralph.pose("walk",5)
                self.isMoving = False
        
        # If the camera is too far from ralph, move it closer.
        # If the camera is too close to ralph, move it farther.
        
        # Now check for collisions.
         
        self.game_obj.CTrav.traverse(render)
        # Adjust ralph's Z coordinate.  If ralph's ray hit terrain,
        # update his Z. If it hit anything else, or didn't hit anything, put
        # him back where he was last frame.
        self.player.resetPlayer(startpos)
        # Keep the camera at one foot above the terrain,
        # or two feet above ralph, whichever is greater.
        self.cam.resetCam()
       
        # The camera should look in ralph's direction,
        # but it should also try to stay horizontal, so look at
        # a floater which hovers above ralph's head.
        


        